/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package wifipositioner;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author pla
 */
public class ResultsFile 
{
    private String path;
    
    public ResultsFile(String path)
    {
        this.path = path;
    }
    
    public void WriteResults(Collection<?> results)
    {
        PrintWriter writer = null;
        
        try
        {
            writer = new PrintWriter(path, "UTF-8");
            
            for(Object s : results)                
                writer.println(s.toString());                
            
            System.out.println("Successfully written " + results.size() + " lines into: " + path);
        }
        catch(FileNotFoundException | UnsupportedEncodingException ex)
        {
            System.out.println("Unable to create file: " + path);
            System.out.println("Exception: " + ex.getMessage());
        }
        finally
        {
            if(writer != null) 
                writer.close();
        }
    }
    
    public Collection<PositionCalculationResult> deserializeFile()
    {
        FileInputStream fileInputStream = null;
        BufferedReader reader = null;
        ArrayList<PositionCalculationResult> deserializedResults = new ArrayList<>();
        
        try
        {
            fileInputStream = new FileInputStream(path);
            reader = new BufferedReader(new InputStreamReader(fileInputStream));
            
            String line = reader.readLine();
            deserializedResults.add(PositionCalculationResult.parse(line));
            
            while(line != null)
            {
                line = reader.readLine();
                if(line != null)
                    deserializedResults.add(PositionCalculationResult.parse(line));
            }
            
        }
        catch(Exception ex)
        {
            System.out.println("Unable to read and deserialize file: " + path);
            System.out.println("Exception: " + ex.getMessage());
        }
        finally
        {
            try
            {
                if(reader != null)
                    reader.close();
                if(fileInputStream != null)
                    fileInputStream.close();
            }
            catch(Exception c)
            {
                System.out.println("Failed to close streams while reading/deserializing file: " + path);
                System.out.println("Exception: " + c.getMessage());
            }
        }
        
        return deserializedResults;
    }
    
    public Collection<Double> deserializeDoubleFile()
    {
        FileInputStream fileInputStream = null;
        BufferedReader reader = null;
        ArrayList<Double> deserializedResults = new ArrayList<>();
        
        try
        {
            fileInputStream = new FileInputStream(path);
            reader = new BufferedReader(new InputStreamReader(fileInputStream));
            
            String line = reader.readLine();
            deserializedResults.add(Double.parseDouble(line));
            
            while(line != null)
            {
                line = reader.readLine();
                if(line != null)
                    deserializedResults.add(Double.parseDouble(line));
            }
            
        }
        catch(Exception ex)
        {
            System.out.println("Unable to read and deserialize file: " + path);
            System.out.println("Exception: " + ex.getMessage());
        }
        finally
        {
            try
            {
                if(reader != null)
                    reader.close();
                if(fileInputStream != null)
                    fileInputStream.close();
            }
            catch(Exception c)
            {
                System.out.println("Failed to close streams while reading/deserializing file: " + path);
                System.out.println("Exception: " + c.getMessage());
            }
        }
        
        return deserializedResults;
    }
}
